package com.duan.laugh.msg.config;

import com.duan.laugh.msg.api.constans.MsgConstants;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

import static com.duan.laugh.msg.api.constans.MsgConstants.*;
import static com.duan.laugh.msg.api.constans.MsgConstants.MQ_QUEUE_SYS_MSG_DELAY;


/**
 * 用户消息队列
 *
 * @author duanjw
 */
@Configuration
public class WebSocketUserMsgQueueConfig {

    /**
     * 系统消息队列
     *
     * @return
     */
    @Bean("userMsgQueue")
    public Queue userMsgQueue() {
        return new Queue(MQ_QUEUE_USER_MSG);
    }

    /**
     * 系统消息延迟队列
     *
     * @return
     */
    @Bean("userMsgDelayQueue")
    public Queue userMsgDelayQueue() {
        return new Queue(MQ_QUEUE_USER_MSG_DELAY);
    }

    /**
     * 配置默认的交换机
     *
     * @return
     */
    @Bean("userMsgCustomExchange")
    CustomExchange userMsgCustomExchange() {
        Map<String, Object> args = new HashMap<>(1);
        args.put("x-delayed-type", "direct");
        //参数二为类型：必须是x-delayed-message
        return new CustomExchange(MQ_EXCHANGE_USER_MSG_DELAY, "x-delayed-message", true, false, args);
    }

    /**
     * 绑定队列到交换器
     *
     * @param queue
     * @param exchange
     * @return
     */
    @Bean("userMsgBinding")
    Binding binding(@Qualifier("userMsgQueue") Queue queue, @Qualifier("userMsgCustomExchange") CustomExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(MQ_QUEUE_USER_MSG_DELAY).noargs();
    }
}
